package com.nine.demo.quartzdemo.service.impl;

import com.nine.demo.quartzdemo.job.TestJob;
import com.nine.demo.quartzdemo.service.QuartzService;
import org.quartz.*;
import org.quartz.impl.StdScheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description:
 * @Author: 刘超
 * @Date: 2023-05-26 15:47
 */
public class QuartzServiceImpl implements QuartzService {
    private static final ThreadLocal<SimpleDateFormat> threadSafeFormat = new ThreadLocal<>();
    private static final String MESSAGE_FORMAT = "yyyy-MM-dd HH:mm:ss.ms";

    private StdScheduler scheduler;

    public StdScheduler getScheduler() {
        return scheduler;
    }

    public void setScheduler(StdScheduler scheduler) {
        this.scheduler = scheduler;
    }

    @PostConstruct
    public void init() throws SchedulerException {
        scheduler.start();
    }
    @PreDestroy
    public void Destory(){
        scheduler.shutdown();
    }

    @Override
    public String start() throws SchedulerException {
        SimpleDateFormat format = threadSafeFormat.get();
        if (format == null) {
            format = new SimpleDateFormat(MESSAGE_FORMAT);
            // 并发环境下，将format设置到自己的线程内
            threadSafeFormat.set(format);
        }
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                .withIdentity("job1","group1")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(10)
                        .repeatForever())
                .build();
        Date date = scheduler.scheduleJob(jobDetail, trigger);
        return format.format(date);
    }

    @Override
    public void stop() {
        if (scheduler.isInStandbyMode()) {
            return;
        }
        scheduler.standby();
    }

    @Override
    public void pause() throws SchedulerException {
        JobKey jobKey = new JobKey("job1","group1");
        scheduler.pauseJob(jobKey);
    }

    @Override
    public void resume() throws SchedulerException {
        JobKey jobKey = new JobKey("job1","group1");
        scheduler.resumeJob(jobKey);
    }

    @Override
    public void delete() throws SchedulerException {
        JobKey jobKey = new JobKey("job1","group1");
        scheduler.deleteJob(jobKey);
    }

    @Override
    public void add() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                .withIdentity("job1","group1")
                .build();
        scheduler.addJob(jobDetail,true,true);
    }

    @Override
    public void triggerJob() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                .withIdentity("job1","group1")
                .build();
        scheduler.triggerJob(jobDetail.getKey());
    }


}
